package com.fenghongzhang.kotlin1903.room

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.room.Room
import com.fenghongzhang.kotlin1903.App
import com.fenghongzhang.kotlin1903.R
import kotlinx.android.synthetic.main.activity_room.*
import kotlin.concurrent.thread

class RoomActivity : AppCompatActivity() {

    lateinit var student:Student

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_room)
////

        val userDao = UserDataBase.getDatabase(this).getUserDao()
        val studentDao = UserDataBase.getDatabase(this).getStudentDao()

        var userId:Long = 0
        insert_obj.setOnClickListener{
            val user = User("xiaoming")
            userDao.insertUser(user)
             student = Student("daming", 15)
            userId = studentDao.insertStudent(student)
            Log.i("id",userId.toString())
        }


        update.setOnClickListener{
            student.id = userId
            student.name = "haha"
            studentDao.updateStudent(student)
        }

        del.setOnClickListener{
            student.id = userId
            studentDao.deleteUser(student)
        }

        query.setOnClickListener{
//            val selectAll = studentDao.selectAll()
//            Log.i("da",selectAll.size.toString())
            val selectStudentByName = studentDao.selectStudentByName("haha")
            Log.i("name",selectStudentByName.toString())
        }




//        val userDao = UserDataBase.getDatabase(this).getUserDao()
//        val user = User("xiaoming")
//        userDao.insertUser(user)
////        //查询
//        val selectAll = userDao.selectAll()
//        Log.i("sql",selectAll.size.toString())
    }
}
